/** The FieldSelectorPanel manages,displays,saves the entries of the item Fields and creates the
 * GUI Elements thanks the the entry classes of the package
 * @author zulianp@lu.unisi.ch & lemmar@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui.itemeditor.fieldselector;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import javax.swing.JPanel;
import ch.unisi.inf.projects.java.uom.*;
import ch.unisi.inf.projects.java.uom.elements.*;
import ch.unisi.inf.projects.java.uom.exceptions.*;
import ch.unisi.inf.projects.java.uom.gui.NameController;

public final class FieldSelectorPanel extends JPanel{

	private static final long serialVersionUID = -1L;
	private Item item;
	private Map<String,String> fields;
	private List<String> keysFromTable;
	private ArrayList<EntryGUI> entries;
	
	public FieldSelectorPanel(final Item item, final List<String> keys)
	
	{
		if(item != null) {
			this.item = item;
			fields = (HashMap<String, String>) item.getFields();
			keysFromTable = keys;
			this.setLayout(new GridLayout(0,2));
			entries = new ArrayList<EntryGUI>();
			setUp();
		}
	}
	
	public void setUp()
	{
		final Parser parser = Parser.instance();
		final NameController nameController = NameController.instance();
		for (final String key : keysFromTable)
		{ 
			try {
				if(nameController.isEditableField(key) && !parser.dbColumnTypeIsList(key) && parser.dbGetVarType(key).equals(Constants.ITEM_CLASS_STRING.value()))
					entries.add(new StringEntryGUI(key,fields.get(key),this,item));
			}
			catch (final UOMException uomex) 
			{
				continue;
			}
		}
		for (final String key : keysFromTable) {
			try {
				if (nameController.isEditableField(key) && !parser.dbColumnTypeIsList(key) && !parser.dbGetVarType(key).equals(Constants.ITEM_CLASS_STRING.value()))
					entries.add(new ItemEntryGUI(key,fields.get(key),this,item));
			}
			catch (final UOMException uomex) 
			{
				continue;
			}
		}
		for (final String key : keysFromTable) {
			if (nameController.isEditableField(key) && parser.dbColumnTypeIsList(key))
				entries.add(new ListEntryGUI(key,fields.get(key),this,item));
		}
	}
	
	public void save (final Multimedia preview, final List<? extends Multimedia> multimedia, final boolean forceSave)
	{
		if(item != null){
			boolean modified = false;
			boolean correct = true;
			for(final EntryGUI e : entries)
			{
				boolean isCorrect = e.setFromInput();
				correct = correct & isCorrect;
			}
			if(correct) {
				for(final EntryGUI e : entries){
					e.save();
					modified = modified || e.isModified();
				}
				if(modified || forceSave) {
					try {
						if (!item.isNew() && ItemManager.instance().itemGet(item.getId(), item.getItemClass()) == null)
							return;
						String ids = new String();
						for (final Multimedia m : multimedia)
						{
							ItemManager.instance().multimediaSave(m);
							ids += (m.getId() + Constants.ITEM_PARSECHAR.value());
						}
						if (ids.length() > 1)
							ids = ids.substring(0,ids.length() - 1);
						else
							ids = "";
						final String prevId;
						if (preview == null)
							prevId = "";
						else
							prevId = preview.getId();
						item.setField("MULTIMEDIA_PREVIEW", prevId);
						item.setField("LISTMULTIMEDIA_MULTIMEDIA", ids);
						ItemManager.instance().elementSave(item);
					} 
					catch (final Exception e) {
						return;
					}
				}
			}
		}
	}
	
	public void saveFromMenu (final Multimedia preview, final List<? extends Multimedia> multimedia, final boolean forceSave)
	{
		save(preview, multimedia, forceSave);
		for(final EntryGUI e : entries)
		{
			e.refresh();
		}
		revalidate();
		repaint();
	}
	
	
		
}
